package com.microinvest.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StockPriceUtil {

    public static List<Double> CreatStockPrice(double FirstPrice,int TotalTurnNum,int EveryTurnNum) {
        List<Double> dayprice =new ArrayList<>();
        DecimalFormat df = new DecimalFormat("#.##");
        double a=FirstPrice;
        int lower=5;
        int higher=5;
        int range;
        Random rand =new Random();
        int ran;
        int largechange;
        for (int i = 0; i < TotalTurnNum; i++) {
            range=rand.nextInt(2)+0;
            if(range==1){
                ran=rand.nextInt(3)-1;
            }
            else{
                ran=rand.nextInt(3)-2;
            }

            largechange=rand.nextInt(31) + 0;
            for (int j = 0; j < EveryTurnNum; j++) {

                if (ran <0) { //偏向下降
                    if(largechange!=0) {
                        lower = 7;
                        higher = 3;
                        a = rand.nextDouble(lower + higher + 1) + (a - lower);
                        if(a<10){
                            a=10;
                        }
                        //System.out.println(String.format("%.2f", a));
                    }
                    else
                    {
                        lower = 10;
                        higher = 0;
                        a = rand.nextDouble(lower + higher + 1) + (a - lower);
                        if(a<10){
                            a=10;
                        }
                        //System.out.println(String.format("%.2f", a));
                    }
                } else if (ran >0) {//偏向上升
                    if (largechange!=1) {
                        higher = 7;
                        lower = 3;
                        a = rand.nextDouble(lower + higher + 1) + (a - lower);
                        if(a<10){
                            a=10;
                        }
                        //System.out.println(String.format("%.2f", a));
                    }
                    else if (ran == 0){
                        lower = 0;
                        higher = 10;
                        a = rand.nextDouble(lower + higher + 1) + (a - lower);
                        if(a<10){
                            a=10;
                        }
                        //System.out.println(String.format("%.2f", a));
                    }
                }
                else{  //偏向稳定，向上？
                    lower = 5;
                    higher = 5;
                    a = rand.nextDouble(lower + higher + 1) + (a - lower);
                    if(a<10){
                        a=10;
                    }

                }

                dayprice.add(Double.valueOf(df.format(a)));
            }

        }
        return dayprice;
    }

    public static double RandowANum(double FirstPrice) {
        double a=FirstPrice;
        int lower=5;
        int higher=5;
        int range;
        Random rand =new Random();
        int ran;
        int largechange;
        for (int i = 0; i < 1; i++) {
            range=rand.nextInt(2)+0;
            if(range==1){
                ran=rand.nextInt(3)-1;
            }
            else{
                ran=rand.nextInt(3)-2;
            }

            largechange=rand.nextInt(31) + 0;
            for (int j = 0; j < 1; j++) {

                if (ran <0) { //偏向下降
                    if(largechange!=0) {
                        lower = 7;
                        higher = 3;
                        a = rand.nextDouble(lower + higher + 1) + (a - lower);
                        if(a<10){
                            a=10;
                        }
                        //System.out.println(String.format("%.2f", a));
                    }
                    else
                    {
                        lower = 10;
                        higher = 0;
                        a = rand.nextDouble(lower + higher + 1) + (a - lower);
                        if(a<10){
                            a=10;
                        }
                        //System.out.println(String.format("%.2f", a));
                    }
                } else if (ran >0) {//偏向上升
                    if (largechange!=1) {
                        higher = 7;
                        lower = 3;
                        a = rand.nextDouble(lower + higher + 1) + (a - lower);
                        if(a<10){
                            a=10;
                        }
                        //System.out.println(String.format("%.2f", a));
                    }
                    else if (ran == 0){
                        lower = 0;
                        higher = 10;
                        a = rand.nextDouble(lower + higher + 1) + (a - lower);
                        if(a<10){
                            a=10;
                        }
                        //System.out.println(String.format("%.2f", a));
                    }
                }
                else{  //偏向稳定，向上？
                    lower = 5;
                    higher = 5;
                    a = rand.nextDouble(lower + higher + 1) + (a - lower);
                    if(a<10){
                        a=10;
                    }

                }

            }

        }
        BigDecimal bd = new BigDecimal(a);
        bd = bd.setScale(2, RoundingMode.HALF_UP); // 四舍五入，保留两位小数
        return bd.doubleValue();
    }
}
